package com.qlogic.commons.xml.support;

import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
import java.util.Hashtable;

public class DefaultFilter extends Hashtable implements Filter {
	/**
	 * 
	 */
	private static final long serialVersionUID = 6178653047538035617L;

	{
		addKey("\"", PrinterEntities.QUOT);
		addKey("&", PrinterEntities.AMP);
		addKey("<", PrinterEntities.LT);
		addKey(">", PrinterEntities.GT);
	}

	public DefaultFilter() {
		super(4);
	}

	/** Register things to be filtered. */
	public Filter addKey(String name, Object keyObj) {
		this.put(name, keyObj);
		return this;
	}

	/** Remove things to be filtered. */
	public Filter removeKey(String name) {
		try {
			this.remove(name);
		} catch (Exception e) {
		}
		return this;
	}

	/** Check to see if something is going to be filtered. */
	public boolean hasKey(String key) {
		return (this.containsKey(key));
	}

	/** Perform the filtering operation. */
	public String scan(String toScan) {
		if (toScan == null || toScan.length() == 0)
			return "";
		StringBuffer bs = new StringBuffer(toScan.length() + 50);
		StringCharacterIterator sci = new StringCharacterIterator(toScan);
		String tmp = null;
		for (char c = sci.first(); c != CharacterIterator.DONE; c = sci.next()) {
			tmp = String.valueOf(c);
			if (hasKey(tmp))
				tmp = (String) this.get(tmp);
			bs.append(tmp);
		}
		return (bs.toString());
	}
}